



options(scipen=999)
args = commandArgs(trailingOnly=TRUE)
# inputPrefix = "imputed/10000/3/20/peel.10000.3.0.20.dosages"
inputPrefix = args[1]
outputName = args[2]

###Dosages
library(data.table)
true.children = as.matrix(fread("genotypes.children"))
true.parents = as.matrix(fread("genotypes.parents"))

true = rbind(true.parents, true.children)
imputed = as.matrix(fread(inputPrefix))

pedigree = read.table("pedigree.txt")

assessDosages = function(id, true, imputed){ 


    father = pedigree[pedigree[,1] == id,2]
    mother = pedigree[pedigree[,1] == id,3]
    parentAverage = (true[true[,1] == mother, -1] + true[true[,1] == father,-1])/2

    
    trueMinusPa = true[true[,1] == id,-1]-parentAverage
    matMinusPa = imputed[imputed[,1] == id,-1]-parentAverage
    val = cor(trueMinusPa, matMinusPa, use = "pair")
    return(val)
}

vals = sapply(true.children[,1], assessDosages, true=true, imputed=imputed)
write.table(vals, outputName, row.names=F, col.names=F, quote=F)
